package net.uhuijia.exception;

import net.uhuijia.enums.ErrorCodeEnum;
import net.uhuijia.vo.ResultMapVo;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class BusinessExceptionHandler {

    /**
     * 参数非法异常.
     *
     * @param e the e
     *
     * @return the wrapper
     */
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ResultMapVo<?> illegalArgumentException(IllegalArgumentException e) {
        return ResultMapVo.wrap(ErrorCodeEnum.param_error.getCode(),ErrorCodeEnum.param_error.getMsg());
    }

    /**
     * 业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ResultMapVo<?> businessException(BusinessException e) {
        return ResultMapVo.wrap(e.getCode(), e.getMessage());
    }

    /**
     * 全局异常.
     *
     * @param e the e
     *
     * @return the wrapper
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public ResultMapVo<?> exception(Exception e) {
        //TODO 记录异常日志
        return ResultMapVo.wrap(ErrorCodeEnum.server_error.getCode(),ErrorCodeEnum.server_error.getMsg());
    }

}
